(function() {
  var __hasProp = {}.hasOwnProperty,
    __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; };

  define(['chaplin', 'lib/dao_factory', 'daos/user_dao'], function(Chaplin, DaoFactory) {
    'use strict';
    var Dispatcher, UserDao, _ref;
    UserDao = null;
    return Dispatcher = (function(_super) {
      var CONTROLLER_ROLES_MAP;

      __extends(Dispatcher, _super);

      function Dispatcher() {
        _ref = Dispatcher.__super__.constructor.apply(this, arguments);
        return _ref;
      }

      Dispatcher.prototype.ROUTES_TO_NOT_CHECK = ['user#login', 'user#novo', 'site#index'];

      CONTROLLER_ROLES_MAP = {
        'user': ['ROLE_ADMINISTRADOR']
      };

      Dispatcher.prototype.initialize = function() {
        Dispatcher.__super__.initialize.apply(this, arguments);
        return UserDao = DaoFactory.getDAOByName('user_dao');
      };

      Dispatcher.prototype.dispatch = function(route, params, options) {
        var isNotVerifiable, promise,
          _this = this;
        isNotVerifiable = this.ROUTES_TO_NOT_CHECK.indexOf(route.name) >= 0;
        if (isNotVerifiable) {
          this.backupGlobals();
          this.publishEvent('layout:updateMenu');
          Dispatcher.__super__.dispatch.apply(this, arguments);
          return this.trackPageview();
        } else {
          this._args = arguments;
          promise = UserDao.verificarUsuarioLogado();
          return promise.done(function(userData) {
            _this.backupGlobals();
            window.AppData.CNTRL_GLOBALS.userData = userData;
            _this.publishEvent('layout:updateMenu', userData.authorities);
            window.userAuthorities = userData.authorities;
            Dispatcher.__super__.dispatch.apply(_this, _this._args);
            return _this.trackPageview();
          });
        }
      };

      Dispatcher.prototype.backupGlobals = function() {
        if (!this.currentController) {
          return;
        }
        if (_.isObject(this.currentController.globals) && !_.isEmpty(this.currentController.globals)) {
          return _.extend(window.AppData.CNTRL_GLOBALS, this.currentController.globals);
        }
      };

      Dispatcher.prototype.trackPageview = function() {
        var error, url;
        url = Backbone.history.getFragment();
        if (!/^\//.test(url) && url !== '') {
          url = "/" + url;
        }
        try {
          return ga('send', {
            hitType: 'pageview',
            page: url
          });
        } catch (_error) {
          error = _error;
        }
      };

      return Dispatcher;

    })(Chaplin.Dispatcher);
  });

}).call(this);
